﻿using System;
using System.Globalization;
using System.Reflection;

using Microsoft.Win32;

namespace Microsoft.Coding4Fun.TweetCraft.Tray
{
    public sealed partial class TweetCraftApplication
    {
        private const string RunKey = @"Software\Microsoft\Windows\CurrentVersion\Run";
        private const string RunValueName = "TweetCraft";

        private static void UpdateRunOnStartupRegistry(bool runOnStartup)
        {
            using (RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(RunKey))
            {
                if (runOnStartup)
                    registryKey.SetValue(RunValueName, String.Format(CultureInfo.InvariantCulture, "\"{0}\" /silent", GetExecutablePath()));
                else
                    registryKey.DeleteValue(RunValueName, false);
            }
        }

        private static bool IsRunOnStartupEnabled()
        {
            using (RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(RunKey))
            {
                return registryKey.GetValue(RunValueName) != null;
            }

        }

        private static string GetExecutablePath()
        {
            return Assembly.GetEntryAssembly().Location;
        }
    }
}
